package net.atomarrow.db.orm.listener;

import java.util.ArrayList;
import java.util.List;

public class OrmListenerKeeper {

    private List<OrmListener> listeners;

    public OrmListenerKeeper() {
        listeners=new ArrayList<OrmListener>();
    }
    public List<OrmListener> getListeners() {
        return listeners;
    }

    public void setListeners(List<OrmListener> listeners) {
        this.listeners = listeners;
    }
    
    public OrmListener getListener(Class clazz){
        for(OrmListener listener:listeners){
            if(listener.isListen(clazz)){
                return listener;
            }
        }
        return null;
    }

    public void loadListeners(List<String> paths) {
        Class clazz;
        try {
            for (String path : paths) {
                clazz = Class.forName(path);
                if(OrmListener.class.isAssignableFrom(clazz)){
                    listeners.add((OrmListener) clazz.newInstance());
                }
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } 
    }
    
}
